package com.group02.community.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.group02.community.entity.owner.ZyOwner;
import com.group02.community.entity.vo.OwnerVO;
import com.group02.community.service.OwnerService;
import com.group02.community.utils.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Api
@RestController
public class OwnerController {

    @Autowired
    private OwnerService ownerService;

    /**
     * 模糊查询+分页数据
     * @param current
     * @param size
     * @param ownerVO
     * @return
     */
    @ApiOperation("分页获取数据")
    @GetMapping("/owners/page")
    public ResponseResult<IPage<ZyOwner>> getOwnerPage(Long current, Long size, OwnerVO ownerVO){
        IPage<ZyOwner> page = ownerService.getOwnerPage(current, size, ownerVO);
        return ResponseResult.ok(page);
    }

    /**
     * 解除业主与房屋的绑定
     * 逻辑：通过给定的修改房屋绑定表中业主id为 “ ” 即可解除绑定
     * @return
     */
    @ApiOperation("解绑业主房屋")
    @PostMapping("/owner/{ownerRoomId}")
    public ResponseResult<String> unbindRoomWithOwner(@PathVariable String ownerRoomId){
        ownerService.updateRoomById(ownerRoomId);
        return ResponseResult.ok("解绑成功");
    }

    @ApiOperation("导出文件")
    @GetMapping("/owner/excel")
    public ResponseResult<String> uploadExcel(HttpServletResponse response) throws IOException {
        ownerService.uploadExcel(response);
        return ResponseResult.ok("导出成功");
    }

    /**
     * 获取小程序传来的用户登陆数据的手机号与密码
     * 来验证用户是否存在并返回数据给小程序
     *
     * @param
     * @return
     */
    @GetMapping("/uniApp/login")
    public ResponseResult<ZyOwner> uniAppLogin(String phoneNumber,String password){
        ZyOwner ownerInfo = ownerService.getOwnerInfo(phoneNumber, password);
        return ResponseResult.ok(ownerInfo);
    }
}
